package EjemploSwingComponentes;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.util.*;

public class SplitPaneDemo implements ListSelectionListener {
    private JLabel picture;
    private JList list;
    private JSplitPane splitPane;
    private Vector v;

    public SplitPaneDemo() {
    	ImageIcon image1 = new ImageIcon("Java.jpg");
    	picture = new JLabel(image1);
    
    	v = new Vector();
    	v.add("Java.jpg");
    	v.add("youngdad.jpeg");
    	v.add("Follow-me-Twitter.jpg");
    	v.add("twitter.jpg");
    	v.add("Facebook.png");
        
        list = new JList(v);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.addListSelectionListener(this);
        JScrollPane listScrollPane = new JScrollPane(list);
        JScrollPane listScrollPane2 = new JScrollPane(picture);
    
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(listScrollPane);
        splitPane.setRightComponent(listScrollPane2);
        splitPane.setOneTouchExpandable(true);

        splitPane.setDividerLocation(150);
        splitPane.setDividerSize(10);

        splitPane.setPreferredSize(new Dimension(400, 200));
    }

    public JList getImageList() {
        return list;
    }

    public JSplitPane getSplitPane() {
        return splitPane;
    }

    public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting())
            return;

        JList theList = (JList)e.getSource();
        if (theList.isSelectionEmpty()) {
            picture.setIcon(null);
        } else {
            int index = theList.getSelectedIndex();
            ImageIcon newImage = new ImageIcon((String)v.elementAt(index));
            System.out.println((String)v.elementAt(index));
            picture.setIcon(newImage);
            picture.setPreferredSize(new Dimension(newImage.getIconWidth(),
                                               newImage.getIconHeight() ));
            picture.revalidate();
        }
    }

    public static void main(String s[]) {
        JFrame frame = new JFrame("SplitPaneDemo");

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });

        SplitPaneDemo splitPaneDemo = new SplitPaneDemo();
        frame.getContentPane().add(splitPaneDemo.getSplitPane());
        frame.pack();
        frame.setVisible(true);
    }
}